package com.feonix.common.basedao;

public class SqlBuilder {

	public static String createSeq(String seqName) {
		String str = "CREATE SEQUENCE \"" + seqName.toUpperCase() + "_SEQ\" MINVALUE 1 "
				+ "MAXVALUE 9999999999999999999999999999 " + "INCREMENT BY 1 START WITH 1 CACHE 20"
				+ " ORDER NOCYCLE\n";
		return str;
	}

	public static String createTrigger(String triggerName, String tableName, String seqName,
			String columnName) {
		StringBuffer sb = new StringBuffer(0);
		sb.append("CREATE OR REPLACE TRIGGER \"" + triggerName.toUpperCase() + "_SETID\"");
		sb.append("BEFORE INSERT ON \"" + tableName.toUpperCase() + "\" FOR EACH ROW\n");
		sb.append("DECLARE\n");
		sb.append("next_id NUMBER;\n");
		sb.append("BEGIN\n");
		sb.append("SELECT " + seqName.toUpperCase() + "_SEQ.NEXTVAL\n");
		sb.append("INTO next_id\n");
		sb.append("FROM dual;\n");
		sb.append(":new." + columnName + ":= next_id;\n");
		sb.append(" END;\n");
		String str = sb.toString();
		sb.delete(0, sb.length());
		return str;
	}

	public static String enableTrigger(String triggerName) {
		StringBuffer sb = new StringBuffer(0);
		sb.append("ALTER TRIGGER \"" + triggerName.toUpperCase() + "_SETID\" ENABLE\n");
		String str = sb.toString();
		sb.delete(0, sb.length());
		return str;
	}

	public static String disableTrigger(String triggerName) {
		return "ALTER TRIGGER " + triggerName.toUpperCase() + "_SETID DISABLE\n";
	}

	public static String removeTrigger(String triggerName) {
		return "DROP TRIGGER " + triggerName.toUpperCase() + "_SETID\n";
	}

	public static String removeTable(String tableName) {
		return "DROP TABLE " + tableName;
	}

	public static String removeSeq(String seqName) {
		return "DROP SEQUENCE " + seqName.toUpperCase() + "_SEQ\n";
	}

}
